<?php

namespace App\Models\Factory;

use App\Helpers\DateUtils;
use App\Helpers\Http\HttpClient;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\CoinDrawLog;
use App\Models\Orm\CoinLog;
use App\Models\Orm\CoinStoreLog;
use App\Models\Orm\Sign;
use App\Models\Orm\SmsConfig;
use App\Models\Orm\SystemConfig;
use App\Models\Orm\UserCoinLog;
use App\Strategies\SmsStrategy;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * 币
 * Class SmsFactory
 * @package App\Models\Factory
 */
class CoinFactory extends AbsModelFactory
{
    public static function addCoinLog($user_id,$type,$number,$total)
    {
        $data['user_id'] = $user_id;
        $data['type'] = $type;
        $data['number'] = $number;
        $data['total'] = $total;
        $data['created_time'] = DateUtils::getToday();
        $result = CoinLog::create($data);
        return $result ? true : false;
    }


    //插入存币记录
    public static function addCoinStoreLog($user_id,$info,$total_bi)
    {
        $data['user_id'] = $user_id;
        $data['number'] = isset($info['number']) ? $info['number'] : 0;
        $data['wallet'] = isset($info['wallet']) ? $info['wallet'] : 0;
        $data['total_bi'] = $total_bi;
        $data['customer_wallet'] = isset($info['customer_wallet']) ? $info['customer_wallet'] : 0;
        $data['type'] = isset($info['type']) ? $info['type'] : 0;
        $data['money'] = isset($info['money']) ? $info['money'] : 0;
        $data['order_id'] = Utils::createNid();
        $data['status'] = CoinStoreLog::IGNORE;
        $data['created_at'] = DateUtils::getToday();
        $data['create_ip'] = Utils::ipAddress();
        $CoinLogId = CoinStoreLog::insertGetId($data);
        return $CoinLogId ? $CoinLogId : false;
    }


    public static function addUserCoinLog($user_id,$number,$store_id,$action,$status)
    {
        $data['user_id'] = $user_id;
        $data['number'] = $number;
        $data['order_id'] = $store_id;
        $data['status'] = $status;
        $data['action'] = $action;
        $data['created_at'] = DateUtils::getToday();
        $result = UserCoinLog::create($data);
        return $result ? true : false;
    }


    //获取生成订单
    public static function getCoinStoreLog($id)
    {
        $result = CoinStoreLog::where('id',$id)->first();
        return $result ? $result : false;
    }

    //前面是否有未处理的申请订单
    public static function hasCoinStoreLog($user_id)
    {
        $result1 = CoinStoreLog::where('user_id',$user_id)->where('type',1)->where('status',CoinStoreLog::APPLY)->first();
        $result2 = CoinStoreLog::where('user_id',$user_id)->where('type',2)->where('status',CoinStoreLog::APPLY)->first();
        return ($result1 && $result2) ? true : false;
    }


    //修改状态
    public static function updateCoinStore($id,$info,$user_id)
    {
        $data['name'] = isset($info['name']) ? $info['name'] : 0;
        $data['status'] = CoinStoreLog::APPLY;
        $data['updated_at'] = DateUtils::getToday();
        $data['pay_type'] = isset($info['pay_type']) ? $info['pay_type'] : 0;
        $result = CoinStoreLog::where('order_id',$id)->where('user_id',$user_id)->update($data);
        return $result ? true : false;
    }


    //删除
    public static function delCoinStore($orderId,$user_id)
    {

        $result = CoinStoreLog::where('order_id',$orderId)->where('user_id',$user_id)->delete();
        return $result ? true : false;
    }


    //删除
    public static function delUserCoin($id,$user_id)
    {

        $result = UserCoinLog::where('order_id',$id)->where('user_id',$user_id)->delete();
        return $result ? true : false;
    }



    //存币记录

    public static function getStoreList($user_id)
    {
        $datas  = CoinStoreLog::where('user_id',$user_id)->orderBy('updated_at','desc')->get();

        foreach ($datas as $k => $data)
        {
            $datas[$k]['remark'] = CoinStoreLog::getActionText($data->status);
        }

        return $datas ? $datas : false;
    }

    //前面是否有未处理的申请订单
    public static function hasCoinDrawLog($user_id)
    {
        $result = CoinDrawLog::where('user_id',$user_id)->where('status',CoinStoreLog::APPLY)->first();
        return $result ? true : false;
    }

    //插入提币记录
    public static function addCoinDrawLog($user_id,$number,$total_bi)
    {
        $data['user_id'] = $user_id;
        $data['number'] = $number;
        $data['total_bi'] = $total_bi;
        $data['fee'] = $number * CoinDrawLog::FEE;
        $data['order_id'] = Utils::createNid();
        $data['status'] = CoinDrawLog::IGNORE;
        $data['created_at'] = DateUtils::getToday();
        $data['create_ip'] = Utils::ipAddress();
        $CoinLogId = CoinDrawLog::insertGetId($data);
        return $CoinLogId ? $CoinLogId : false;
    }


    //获取生成订单
    public static function getCoinDrawLog($id)
    {
        $result = CoinDrawLog::where('id',$id)->first();
        return $result ? $result : false;
    }

    //修改状态
    public static function updateCoinDraw($id,$status)
    {
        $data['status'] = $status;
        $data['updated_at'] = DateUtils::getToday();
        $result = CoinDrawLog::where('id',$id)->update($data);
        return $result ? true : false;
    }


    //存币记录

    public static function getDrawList($user_id)
    {
        $datas  = CoinDrawLog::where('user_id',$user_id)->orderBy('updated_at','desc')->get();

        foreach ($datas as $k => $data)
        {
            $datas[$k]['remark'] = CoinDrawLog::getActionText($data->status);

        }

        return $datas ? $datas : false;
    }


}